<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-16
 * Time: 17:16
 */

namespace app\api\controller\v1;

use app\api\model\MdbUser;
use app\api\model\MdbUserMoney;
use app\api\model\UserMoney;
use app\api\service\member\UpdateUser;
use app\api\validate\member\CheckGrade;

class User
{
    /**
     * 获取用户列表
     * @validate('PagingParameter')
     */
    public function getUserList($page,$limit,$nickname,$user_no)
    {
        $where[] = ['mdb_user.id', '<', 30841];
        if($nickname) {
            $where[] = ['mdb_user.nickname','like','%'.$nickname.'%'];
        }
        $query = MdbUser::hasWhere('wechat',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('title','like','%'.$user_no.'%');
            }
        })->with('wechat')->where($where)->order('mdb_user.id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '信息未同步';
            } else {
                $item['status_name'] = '信息已同步';
            }
            if($item['sex'] == 1) {
                $item['sex_name'] = '男';
            } else {
                $item['sex_name'] = '女';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }
    /**
     * 获取余额列表
     * @validate('PagingParameter')
     */
    public function getMoneyList($page,$limit,$user_id)
    {
        $query = MdbUserMoney::where('mdb_user_id',$user_id)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['type'] == 1) {
                $item['type_name'] = '后台操作得到';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '后台操作失去';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 修改用户余额
     */
    public function updateUserMoney($type,$money,$id)
    {
        $updateMoney = new UpdateUser($type,$money,$id);
        return $updateMoney->updateData();
    }
}